//
//  BurnViewController.m
//  LINNER
//
//  Created by wei wang on 2017/3/30.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "BurnViewController.h"
#import "LINNERBurnTime.h"
#import "UserManualViewController.h"
#import "AboutBurntViewController.h"

@interface BurnViewController ()

@property (weak, nonatomic) IBOutlet UIView *circleView;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet UILabel *burnTime;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;

@end

@implementation BurnViewController
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self setNavigationStyle];
  
}

- (void)onRightButtonClick:(id)sender{
    
    AboutBurntViewController *vc = [[AboutBurntViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [AppDelegate globalDelegate].burnMode = self.indexTag;
    
    [[AppDelegate globalDelegate] judgeBurnMusicstage];
    
    [self getTime];

    
    // Do any additional setup after loading the view from its nib.
    //将图层的边框设置为圆脚
    _circleView.layer.cornerRadius = 45;
    
    _circleView.layer.masksToBounds = YES;
    //给图层添加一个有色边框
    
    _circleView.layer.borderWidth = 2;
    
    _circleView.layer.borderColor = [UIColor whiteColor].CGColor;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTime) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTime) name:@"LightingSource" object:nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTime) name:@"stop" object:nil];

    
}

- (void)headset{
    
    if ([AppDelegate globalDelegate].avAudioPlayer.isPlaying==YES) {
        
        self.playBtn.selected = YES;
        
    }else{
        
        self.playBtn.selected = NO;
        [[AppDelegate globalDelegate] pause];
        [[AppDelegate globalDelegate] cancelTimeLighting];
    }

}


- (void)getTime{
    
    if (self.indexTag == 0) {//lightning
        
        if ([AppDelegate globalDelegate].avAudioPlayer.isPlaying==YES) {
            
            self.playBtn.selected = YES;
            
        }else{
            
            self.playBtn.selected = NO;
            [[AppDelegate globalDelegate] cancelTimeLighting];

        }
        
        
#pragma mark - --------------------------进行一次判断 最小时间为分钟，最大时间为小时--------------------------
        NSLog(@"时间时间：%lld",[EXRACT_OBJECT(LIGHTINGTOTALPALTTIME) longLongValue]);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //回调或者说是通知主线程刷新，
            if ([EXRACT_OBJECT(LIGHTINGTOTALPALTTIME) longLongValue]/3600 > 1) {//小时
                
                NSString *str1 = MyNSLocalizedString(@"已煲机时间");
                NSString *hour = [NSString stringWithFormat:@" %02lld",[EXRACT_OBJECT(LIGHTINGTOTALPALTTIME) longLongValue]/3600];
                
                NSString *min = [NSString stringWithFormat:@" %02lld",([EXRACT_OBJECT(LIGHTINGTOTALPALTTIME) longLongValue]%3600)/60];
                
                NSString *str3 = [NSString stringWithFormat:@"%@",MyNSLocalizedString(@"小时")];
                
                NSString *str4 = [NSString stringWithFormat:@"%@",MyNSLocalizedString(@"分钟")];
                
                NSString *str5 = [str1 stringByAppendingString:hour];
                NSString *str6 = [str5 stringByAppendingString:str3];
                
                NSString *str7 = [str6 stringByAppendingString:min];
                
                NSString *end = [str7 stringByAppendingString:str4];
                
                self.burnTime.text = end;
                
            }else{//分钟
                
                
                
                NSString *str1 = MyNSLocalizedString(@"已煲机时间");
                NSString *min = [NSString stringWithFormat:@" %02lld",([EXRACT_OBJECT(LIGHTINGTOTALPALTTIME) longLongValue]%3600)/60];
                
                NSString *hour = [NSString stringWithFormat:@" 00"];

                NSString *str3 = [NSString stringWithFormat:@"%@",MyNSLocalizedString(@"小时")];
                
                NSString *str4 = [NSString stringWithFormat:@"%@",MyNSLocalizedString(@"分钟")];
                
                NSString *str5 = [str1 stringByAppendingString:hour];
                NSString *str6 = [str5 stringByAppendingString:str3];
                
                NSString *str7 = [str6 stringByAppendingString:min];
                
                NSString *end = [str7 stringByAppendingString:str4];
                
                
                self.burnTime.text = end;
                
            }
            self.progressLabel.text = [NSString stringWithFormat:@"%.0f%%",(double)([EXRACT_OBJECT(LIGHTINGTOTALPALTTIME) longLongValue]/129600.0)*100];

            
        });
        

    }else if (self.indexTag == 1){//linner
        
        if ([AppDelegate globalDelegate].avAudioPlayer.isPlaying == YES) {
            
            self.playBtn.selected = YES;
            
        }else{
            
            [[AppDelegate globalDelegate] cancelTimeLighting];
            self.playBtn.selected = NO;
            
        }
        
        
#pragma mark - --------------------------进行一次判断 最小时间为分钟，最大时间为小时--------------------------
        
        NSInteger timeCount;
        NSArray *deviceArray = [LINNERBurnTime selectObjectPredicateWithFormat:[NSString stringWithFormat:@"where macAddress = '%@'",[AppDelegate globalDelegate].accessory.serialNumber]];
        {
            LINNERBurnTime *time = [deviceArray lastObject];
            NSInteger totalTime = [time.time integerValue];
            NSLog(@"这里是linner耳机的煲机时间时间：%ld",(long)totalTime);
            timeCount = totalTime;
        }

        
        dispatch_async(dispatch_get_main_queue(), ^{
            //回调或者说是通知主线程刷新，
            if (timeCount/3600 > 1) {//小时
                
                NSString *str1 = MyNSLocalizedString(@"已煲机时间");
                NSString *hour = [NSString stringWithFormat:@" %02ld",timeCount/3600];
                
                NSString *min = [NSString stringWithFormat:@" %02ld",(timeCount%3600)/60];
                
                NSString *str3 = [NSString stringWithFormat:@" %@",MyNSLocalizedString(@"小时")];
                
                NSString *str4 = [NSString stringWithFormat:@" %@",MyNSLocalizedString(@"分钟")];
                
                NSString *str5 = [str1 stringByAppendingString:hour];
                NSString *str6 = [str5 stringByAppendingString:str3];
                
                NSString *str7 = [str6 stringByAppendingString:min];
                
                NSString *end = [str7 stringByAppendingString:str4];
                
                self.burnTime.text = end;
                
            }else{//分钟
                
                NSString *str1 = MyNSLocalizedString(@"已煲机时间");
                NSString *min = [NSString stringWithFormat:@" %02ld",(timeCount%3600)/60];
                
                NSString *hour = [NSString stringWithFormat:@" 00"];
                
                NSString *str3 = [NSString stringWithFormat:@"%@",MyNSLocalizedString(@"小时")];
                
                NSString *str4 = [NSString stringWithFormat:@"%@",MyNSLocalizedString(@"分钟")];
                
                NSString *str5 = [str1 stringByAppendingString:hour];
                NSString *str6 = [str5 stringByAppendingString:str3];
                
                NSString *str7 = [str6 stringByAppendingString:min];
                
                NSString *end = [str7 stringByAppendingString:str4];
                
                
                
                self.burnTime.text = end;
                
            }
        
            self.progressLabel.text = [NSString stringWithFormat:@"%.0f%%",(double)(timeCount/129600.0)*100];
            
            // 如果正在煲机，获取时间后继续播放
            if ([AppDelegate globalDelegate].isBurn)
            {
                [[AppDelegate globalDelegate] play];
                self.playBtn.selected = YES;
            }

        });

        
    }
    
    
    

    
}

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
  
}
- (IBAction)playEvent:(UIButton *)sender {
    
    
    if ([AppDelegate globalDelegate].avAudioPlayer.isPlaying) {
        
        [[AppDelegate globalDelegate] pause];
        [[AppDelegate globalDelegate] cancelTimeLighting];
        self.playBtn.selected = NO;
        [AppDelegate globalDelegate].isBurn = NO;
        
    }else{
        
        [[AppDelegate globalDelegate] play];
        [[AppDelegate globalDelegate] setTimeLighting];
        self.playBtn.selected = YES;
        [AppDelegate globalDelegate].isBurn = YES;
    }
    
    [self getTime];
}

- (void)setNavigationStyle{
    
//    [self setNavigationBarTitle:MyNSLocalizedString(@"煲机模式")];
    [self setNavigationTitle:MyNSLocalizedString(@"煲机模式") titleFont:[UIFont systemFontOfSize:18]];
    [self setLeftButtonImage:[UIImage imageNamed:@"tittle_back_nor"]];
    [self.navigationController.navigationBar makeTransparent];
    [self setNavigationBarTransparent];
    [self setRightButtonTitle:MyNSLocalizedString(@"煲机说明")];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
